layui.use(['table', 'admin', 'ax', 'func','upload','form','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var upload = layui.upload;
    var form = layui.form;
    var laydate = layui.laydate;

    //渲染时间选择框
    laydate.render({
        elem: '#zdrq',
        trigger: 'click',
        // max: Feng.currentDate()
    });

    /**
     * 管理
     */
    var BtyCrk = {
        tableId: "btyCrkTable"
    };

    form.on('select(flag)', function (data) {
        BtyCrk.search();
    });

    form.on('select(sflb)', function (data) {
        BtyCrk.search();
    });

    /**
     * 初始化收发类别
     */
    var kjAjax = new $ax(Feng.ctxPath + "/btyCrk/getSflb");
    var kjResult = kjAjax.start();
    $.each(kjResult, function (index, val) {
        $("#sflb").append("<option value='" + val.sflb + "'>" + val.sflb + "</option>");
    });
    form.render('select');

    /**
     * 初始化表格的列
     */
    BtyCrk.initColumn = function () {
        return [[
            //{type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'crkrq', title: '出入库日期',width:110},
            // {field: 'djlx', sort: true, title: '单据类型'},
            // {field: 'djh', sort: true, title: '单据号'},
            {field: 'sflb', title: '收发类别',width:120},
            //{field: 'ywlx', sort: true, title: '业务类型'},
            // {field: 'ck', sort: true, title: '仓库'},
            // {field: 'bm', sort: true, title: '部门'},
            // {field: 'kh', sort: true, title: '客户'},
            {field: 'gys', title: '供应商',width:220},
            //{field: 'bz', sort: true, title: '备注'},
            // {field: 'chbm', sort: true, title: '存货编码'},
            // {field: 'chmc', sort: true, title: '存货名称'},
            {field: 'dw',  title: '单位',width:60},
            {field: 'rksl', title: '入库数量',width:90},
            {field: 'cksl', title: '出库数量',width:90},
            {field: 'price',title: '单价',width:80},
            {field: 'rkje',title: '入库金额',width:90},
            {field: 'ckje', title: '出库金额',width:90},
            // {field: 'sfzp', sort: true, title: '是否赠品'},
            {field: 'zdy4',  title: '自定义4',width:90},
            {field: 'zdy5',  title: '自定义5',width:90},
            //{field: 'qzr', sort: true, title: '签字人'},
            {field: 'errormsg',title: '失败原因'},
           // {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyCrk.search = function () {
        var queryData = {};

        queryData['djlx'] = $('#djlx').val();
        queryData['sflb'] = $('#sflb').val();
        queryData['kh'] = $('#kh').val();
        queryData['gys'] = $('#gys').val();
        queryData['flag'] = $('#flag').val();

        table.reload(BtyCrk.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 导入Excel
     */
    var loadIndex;
    upload.render({
        elem: '#btnExcel'
        , url: '/btyCrk/import'
        , accept: "file"
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load('正在导入...', 2, {shade: [0.15, '#ccc']});
            //this.data = {'zdrq': $('#zdrq').val()};//关键代码
        }
        , done: function (res) {
            if (res.state == 0) {
                Feng.error("导入失败!" + res.errmsg);
            }
            if (res == true) {
                alert("导入成功!");
                Feng.success("导入成功!");
                window.location.reload();
            }
            if (res == false) {
                Feng.error("导入失败!");
                window.location.reload();
            }
            layer.close(loadIndex);
        }
    });
    $('#zcpz').click(function () {
        BtyCrk.zcpz();

    });

    BtyCrk.zcpz = function () {
        var zdrq=$("#zdrq").val();
        if(zdrq==""){
            Feng.error("请选择用友制单日期！");
            return;
        }
        //var dm=$("#dm").find("option:selected").text();
        var sflb = $('#sflb').val();
        if(sflb==""){
            Feng.error("请选择收发类别！");
            return;
        }
        // var ajax = new $ax(Feng.ctxPath + "/btyCrk/pz", function (data) {
        //     if(data.code==200){
        //         //Feng.success("凭证生成成功!");
        //         alert("凭证生成完成!");
        //     }else {
        //         alert(data.message);
        //     }
        //     table.reload(BtyCrk.tableId);
        // }, function (data) {
        //     Feng.error("生成失败!" + data.responseJSON.message + "!");
        // });
        // ajax.set("zdrq", zdrq);
        // ajax.set("sflb", sflb);
        // ajax.start();

        $.ajax({
            type: 'post',
            url: Feng.ctxPath +'/btyCrk/pz',
            dataType: 'json',
            async: true,
            data: {
                zdrq:zdrq,
                sflb:sflb
            },
            beforeSend: function (data) {
                loadIndex = layer.load('正在生成...', 2, {shade: [0.15, '#ccc']});
            },
            complete: function(){
                layer.close(loadIndex);
            },
            success: function (data) {
                if(data.code==200){
                    //Feng.success("凭证生成成功!");
                    alert("凭证生成完成!");
                }else {
                    alert(data.message);
                }
                table.reload(BtyCrk.tableId);
            },
            error: function (data) {
                Feng.error("生成失败!" + data.responseJSON.message + "!");
            }
        });
    };
    /**
     * 弹出添加对话框
     */
    BtyCrk.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/btyCrk/add',
            tableId: BtyCrk.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyCrk.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/btyCrk/edit?id=' + data.id,
              tableId: BtyCrk.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyCrk.exportExcel = function () {
        var checkRows = table.checkStatus(BtyCrk.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyCrk.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyCrk/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyCrk.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyCrk.tableId,
        url: Feng.ctxPath + '/btyCrk/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: BtyCrk.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyCrk.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyCrk.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyCrk.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyCrk.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyCrk.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyCrk.onDeleteItem(data);
        }
    });
});
